<?php
namespace ImiApp\Module\Member\Service;

// use Imi\Bean\Annotation\Bean;
use Imi\Server\Session\Session;
use ImiApp\Exception\BusinessException;
use ImiApp\Module\Member\Model\Group;
use ImiApp\Exception\NotFoundException;

use Imi\Db\Db;

// /**
//  * @Bean("GroupService")
//  */
class GroupService
{
    /**
     * 使用用户ID获取记录
     *
     * @param integer $id
     * @return \ImiApp\Module\Member\Model\Group
     */
    public function get(int $id): Group
    {
        var_dump('group_ID',$id);

        $record = Group::find($id);
        if(!$record)
        {
            throw new NotFoundException(sprintf('未找到群id： %s', $id));
        }
        return $record;
    }



    /**
     * 获取用户是否在某个群里面
     *
     * @param int $uid 学生id
     * @param int $group_id 群id
     * @return bool
     */
    public function getUid_is_group($uid,$group_id)
    {
        $db = Db::query()->table('fa_group_users');
        $res = $db->where('uid','=',$uid)->where('group_id','=',$group_id)->select();
        $group = $res->get();
        if($group){
            var_dump("该用户存在该群".$group_id,$group);
            return true;
        }else{
            return false;
        }
    }

    /**
     * 获取该群的用户列表
     *
     * @param int $group_id 群id
     * @return array
     */
    public function getGroupUsers(int $group_id): GroupUsers
    {
        $record = GroupUsers::select([
            'group_id'  =>  $group_id,
        ]);
        if(!$record)
        {
            throw new NotFoundException(sprintf('该群没有用户 %s', $group_id));
        }
        return $record;
    }

    /**
     * 获取群信息
     *
     * @param integer $group_id 群id
     * @return void
     */
    public function getGroupData(int $group_id)
    {
        
        return Group::find([
            'group_id'  =>  $group_id,
        ]);

        return $group;
        
    }
  
    /**
     * 群是否关闭
     *
     * @param int $group_id 群id
     * @return void
     */
    public function GroupIsOff(int $group_id)
    {
        
        $group = $this->getGroupData($group_id);
        if($group['status']=='1'){
            return true;
        }else{
            return false;
        }

        // return $group;
        
    }

}
